package service.Impl;

import context.BaseContext;
import dao.Impl.EmployeeDaoImpl;
import model.VO.EmployeePageVO;
import model.entity.Employee;
import service.EmployeeService;
import util.Encrypt;

import java.util.List;
import java.util.Objects;

public class EmployeeServiceImpl implements EmployeeService {

    EmployeeDaoImpl employeeDao=new EmployeeDaoImpl();
    @Override
    public Employee Find(int id) {
        return employeeDao.Find(id);
    }

    @Override
    public void UpdateStatus(int id) {
        int Status=employeeDao.FindStatus(id);
        if (Status==1){
            employeeDao.UpdateStatus(id,0);
            System.out.println("修改状态为0");
        }
        else {
            employeeDao.UpdateStatus(id,1);
            System.out.println("修改状态为1");
        }
    }

    @Override
    public Employee Find(String username) {
        return employeeDao.Find(username);
    }

    @Override
    public void Create(String idNumber, String name, String phone, String gender, String username) {
        employeeDao.creatEmployee(idNumber,name,phone,gender,username, BaseContext.getCurrentId());
        //System.out.println("新建员工成功!");
    }

    @Override
    public void Update(int id, String idNumber, String name, String phone, String gender, String username) {
        employeeDao.Update(id,idNumber,name,phone,gender,username,BaseContext.getCurrentId());
        System.out.println("更新员工信息成功!");
    }

    @Override
    public void EditPassword(int empId, String newPassword, String oldPassword) {

        String password=employeeDao.Find(empId).getPassword();
        if (Objects.equals(password, Encrypt.toMd5(oldPassword))){
            employeeDao.EditPassword(empId,newPassword);
            System.out.println("密码修改成功!");
        }
        else {
            System.out.println("旧密码出错!"+password);
        }

    }

    @Override
    public EmployeePageVO Page(int page, int pageSize) {
        EmployeePageVO employeePageVO=new EmployeePageVO();
        employeePageVO.setEmployeeList(employeeDao.Page(page,pageSize));
        employeePageVO.setTotal(employeeDao.Page(page,pageSize).size());
        System.out.println("分页查询成功!");
        return employeePageVO;
    }
}
